//在线支付
$(function(){
	canReturn();
	init();
});

function init(){
	
	var sessAppBase = sessionStorage.getItem("appBase");
	var sendData = JSON.parse(sessAppBase);
	padMarketing.ajaxNormal('initPayment.action',sendData,
		function(result){
			var appBase = eval('(' + result.data + ')');
			$("#productName").text(appBase.productName);
			$("#phName").text(appBase.phName);
			$("#policyRef").text(appBase.policyRef);
			$("#nbPremium").text(formatMoney(appBase.nbPremium));
			//支付宝请求参数
			$("#objId").val(appBase.orderId);
			$("#alipayUrl").val(result.alipayUrl);
			$("#wechatUrl").val(result.wechatUrl);
		},
		function(){
			$('#alertMsg_btn3').click(function(){
				window.location.href = '/pad_marketing/ordermanager/index.action';
			});
			//alertMsg("支付初始化异常！");
		});
}

function submitPay(flag){
	var msg="根据保监规定，该保费不得使用信用卡支付。请确认您未使用信用卡支付上述保费，谢谢！";
	alertMsg(msg,1,toPay,flag);
}

/**
 * 提交支付宝或微信
 */

var timer = null;
function toPay(flag){
	var url = "";
	var objId = $("#objId").val();
	var productName = encodeURI($("#productName").text());
	if('wechat'==flag){
		url = $("#wechatUrl").val();
		url = url + "?objType=NOD&objId="+objId + "&productName=" + productName;
	}else if('alipay'==flag){
		url = $("#alipayUrl").val();
		url = url + "?objType=NOD&objId="+objId + "&subject=" + productName+ "&alibody=" + productName;
	}
//	frmAlipay.action = url;
//	$("#frmAlipay").submit();
	
//	window.open(url, "_blank");
	payment('pdf',url,'','N');
	timer = window.setInterval(payComplete, 3000);
}

/**
 * 支付完成按钮
 */
function payComplete(){
	var sessAppBase = sessionStorage.getItem("appBase");
	var sendData = JSON.parse(sessAppBase);
	/*padMarketing.ajaxNormal('payInit.action',sendData,
	function(result){
			var appBase = eval('(' + result.data + ')');
			if (appBase.status == "F") {//已支付
				paySuccess.productName.value = appBase.productName;
				paySuccess.preprintedNo.value = appBase.preprintedNo;
				paySuccess.policyRef.value = appBase.policyRef;
				paySuccess.nbPremium.value = appBase.nbPremium;
				$("#paySuccess").submit();
				window.clearInterval(timer);
				window.location.href='/pad_marketing/main/paySuccess.action?sucFlag=2&preprintedNo='+appBase.preprintedNo+'&policyRef='+appBase.policyRef+'&nbPremium='+appBase.nbPremium+'&productName='+appBase.productName;
			}
		},
		function(){
//			alertMsg("查询订单异常！");
		}, true, false);*/
	$.ajax({
		url:'payInit.action',
		data:{'jsonParam': JSON.stringify(sendData)},
		dataType: 'json',
		async: true,
		type: 'POST',
		success: function(result) {
//			console.log(result);
			var appBase = eval('(' + result.data + ')');
			if (appBase.status == "F") {//已支付
				paySuccess.productName.value = appBase.productName;
				paySuccess.preprintedNo.value = appBase.preprintedNo;
				paySuccess.policyRef.value = appBase.policyRef;
				paySuccess.nbPremium.value = appBase.nbPremium;
				$("#paySuccess").submit();
				window.clearInterval(timer);
				window.location.href='/pad_marketing/main/paySuccess.action?sucFlag=2&preprintedNo='+appBase.preprintedNo+'&policyRef='+appBase.policyRef+'&sign='+appBase.sign+'&appId='+appBase.appId+'&orderId='+appBase.orderId;
			}
		},
		error: function(data) {
			console.log('失败');
			window.clearInterval(timer);
			alertMsg(data.responseText, 0, function() {
				window.location.href='/pad_marketing/ordermanager/index.action';
			});
		}
	});
}

function goBack(){
	
	var url = getArgsFromHref("url");
	if(url==''){
		history.go(-1);
	}else if(url.indexOf("/")>-1){
		window.location.href = "../"+url;
	}else{
		window.location = url;
	}
}

/**
 * 隐藏返回和上一步按钮
 */
function canReturn(){
	var url = getArgsFromHref("url");
	if(url==''){
		$("#return").hide();
//		$("#goback").hide();
	}
}

